<?php


namespace app\admin\controller\anx;

 
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="通知公告")
 */
class Anx extends AdminController
{

    use \app\admin\traits\Curd;
    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\Anx();
        
    }
    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
    * @NodeAnotation(title="添加")
    */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('标题不能为空');
            }
            $data = [
                'title'=>$post['title'],
                'content'=>$post['content'],
                'add_time'=>date('Y-m-d H:i:s',time())
            ];
            try {
                $save = $this->model->save($data);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

     /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('标题不能为空');
            }
            $data = [
                'title'=>$post['title'],
                'content'=>$post['content']
            ];
            try { 
               $this->model->where('id',$post['id'])->update($data);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功');
        }else{
            $post = $this->request->param(); 
  
            $row = $this->model->where('id',$post['id'])->find();
        
            $this->assign('row', $row);
            return $this->fetch();
        }
       
    }

    // //发布通知
    // public function fabu(){
    //     $post = $this->request->param();
    //     if(empty($post['id'])){
    //         $this->error('通知不存在');
    //     }
    //     $arr = $this->model->where('id',$post['id'])->field('title as content,content as title,add_time as time')->find()->toArray();
    //     $user = Db::name('user')->order('id asc')->field('id,wx_openid')->select()->toArray();
    //     foreach($user as $k=>$v){
    //         $openid = $v['wx_openid'];
    //         $adv->real_authentication($openid,$arr,1);
    //     }
    //     $this->model->where('id',$post['id'])->update(['status'=>2]);
    //     $this->success('发布成功');
    // }

 
 
}